#include <stdio.h>
#include <stdlib.h>
#include "lcv/cam.h"
#include "lcv/dip.h"
#include "config.h"

void test_cam()
{
	unsigned short value = 0;
	
	printf("Testing registers:\n");	
	printf("read analog_gain... ");
	fflush(stdout);
	value = lcv_config_read(REG_ANALOG_GAIN);
	printf("value is: %d\n", value);
		
	printf("write analog_gain to 20... ");	
	fflush(stdout);
	if(lcv_config_write(REG_ANALOG_GAIN, 20))
	{
		printf("register written\n");
	} else {
		printf("\nError: register not written\n");
		printf("Aborting Test\n");
		exit(-1);
	}
           
	printf("read analog_gain...");	
	fflush(stdout);
	value = lcv_config_read(REG_ANALOG_GAIN);
	printf("value is: %d\n", value);
	
	// Muss eine Warnung verursachen:
	printf("read illegal register... ");	
	fflush(stdout);
	lcv_config_read(99);
	printf("ok\n");	
	
	printf("write illegal register... ");	
	if(lcv_config_write(99, 20) > 0)
	{
		printf("\nError: Illegal value was not detected\n");
		printf("Aborting Test\n");
		exit(-1);
	} else {
		printf("detected (returned an error-msg)\n");
	}	
	
	
	/* Kamera konfigurieren*/
	printf("configure camera... ");
	fflush(stdout);
	
	/* Reset */
	lcv_config_write(REG_RESET, 1);

	/* Turn on Snapshot mode (set bit 4 high) */
	lcv_config_write(REG_CHIP_CONTROL, 0x398);
	//lcv_config_write(REG_CHIP_CONTROL, 0x388);
	//lcv_config_write(REG_CHIP_CONTROL, 0x288);
			
	/* Color mode (0x0F set bit 2) */
	lcv_config_write(REG_PIXEL_OPERATION_MODE, 0x11); //off (c.f. mt9v032.c driver)
	
	/* Mirror: Row Flip */
	lcv_config_write(REG_READ_MODE, 0x0320);
	
	/* Set AGC/AEC */
	lcv_config_write(REG_AEC_AGC_ENABLE, 0x3);

	/* Set Noise Correction */ 
	lcv_config_write(REG_ROW_NOISE_CORR_CTRL_1, 0x34);
	
	printf("ok\n");
	
		
	printf("Testing Capture-Functions:\n");
	printf("capture frame... ");
	fflush(stdout);
	lcv_picture p_captured = lcv_capture_frame(FULLSIZE_INPUT_IMAGE);
	printf("ok\n");	
	
	/*
	#ifdef LCV_TARGET
		printf("copy to http-folder... ");
		fflush(stdout);
		lcv_write_bmp(p_captured, "/home/httpd/1.bmp");
		printf("ok\n");	
	#endif
	*/
	
	/*
	printf("writing captured frame... ");
	fflush(stdout);
	lcv_write_bmp(p_captured, "cam_capture_frame.bmp");
	printf("ok\n");		
	
	/*
	// Drei mal hintereinander LCV_CAPURE_FRAME ist ok; Vier mal hintereinander verursacht später einen error 
	lcv_free_picture(&p_captured);
	p_captured = lcv_capture_frame(FULLSIZE_INPUT_IMAGE);
	lcv_free_picture(&p_captured);
	p_captured = lcv_capture_frame(FULLSIZE_INPUT_IMAGE);
	lcv_free_picture(&p_captured);
	p_captured = lcv_capture_frame(FULLSIZE_INPUT_IMAGE);
	// lcv_free_picture(&p_captured);
	// p_captured = lcv_capture_frame(FULLSIZE_INPUT_IMAGE);	
	*/
	
	/*
	printf("capture region (FULLSIZE)... ");
	fflush(stdout);
	lcv_free_picture(&p_captured);
	p_captured = lcv_capture_region(1, 4, 752, 480, FULLSIZE_INPUT_IMAGE);
	//lcv_write_bmp(p_captured, "cam_capture_region.bmp");
	printf("ok\n");
	
	/*
	printf("capture region (350 x 223)... ");
	fflush(stdout);
	lcv_free_picture(&p_captured);
	p_captured = lcv_capture_region(1, 4, 350, 223, REGION_INPUT_IMAGE);
	//lcv_write_bmp(p_captured, "cam_capture_region.bmp");
	printf("ok\n");
	
	/*
	printf("capture frame again... ");
	fflush(stdout);
	lcv_free_picture(&p_captured);
	p_captured = lcv_capture_frame(FULLSIZE_INPUT_IMAGE);
	printf("ok\n");	
	*/
		
	/*
	while (TRUE) {
		printf("capture region (FULLSIZE)... ");
		fflush(stdout);
		p_captured = lcv_capture_region(1, 4, 752, 480, FULLSIZE_INPUT_IMAGE);
		printf("ok\n");
		usleep(2000000);
	}
	*/
	
	lcv_free_picture(&p_captured);
}
